package com.flowcloud.kafka.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wuwenbin
 * @Date：Created on 2020/12/18 09:20
 * Description:
 */
@Slf4j
public class UipListenableFutureCallback<T> implements ListenableFutureCallback<T> {

	private long sendTime;
	private Object msg;
	private String topic;

	public UipListenableFutureCallback(String topic, long sendTime, Object msg) {
		this.topic = topic;
		this.sendTime = sendTime;
		this.msg = msg;
	}

	@Override
	public void onFailure(Throwable ex) {
		StringBuilder errorLog = new StringBuilder(300);
		List<Object> logArgs = new ArrayList<>();
		errorLog.append("\n\n==============================  send kafka message log start =========================\n");
		errorLog.append("  topic: {}\n");
		errorLog.append("  message: {}\n");
		errorLog.append("  duration: {}ms\n");
		errorLog.append("  result: failure\n");
		//errorLog.append("  exception: {}\n");
		errorLog.append("===============================  send kafka message log end ================================\n\n");

		logArgs.add(topic);
		logArgs.add(msg);
		logArgs.add(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sendTime));
		//logArgs.add(ex.getMessage());

		log.error(errorLog.toString(), logArgs);
	}

    @Override
    public void onSuccess(T t) {
		StringBuilder successLog = new StringBuilder(300);
		List<Object> logArgs = new ArrayList<>();
		successLog.append("\n\n===============================  send kafka message log start ========================\n");
		successLog.append("  topic: {}\n");
		successLog.append("  message: {}\n");
		successLog.append("  duration: {}ms\n");
		successLog.append("  result: success\n");
		//successLog.append("  {}\n");
		successLog.append("===============================  send kafka message log end ==============================\n\n");

		logArgs.add(topic);
		logArgs.add(msg);
		logArgs.add(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sendTime));
		//logArgs.add(t);
		log.info(successLog.toString(), logArgs.toArray());
	}
}
